package com.cheems.cheemsspringaialibaba.Tools;

import org.springframework.ai.chat.model.ToolContext;
import org.springframework.ai.tool.annotation.Tool;

import java.util.Map;

//查询学生成绩
public class SearchExam {

    private final String appid = "nd9c7a7f4aea7c09c4";

    private final String secret = "2b27ac67656eb1c0d829dbe582cd7a03";

    @Tool(description = "查询成绩")
    public String SearchExam(ToolContext toolContext) {
        // 获取参数
        Map<String, Object> context = toolContext.getContext();
        String userName = (String)context.get("userName");
        String henauNumber = (String)context.get("henauNumber");

        String url = String.format("https://oauth.henau.edu.cn/oauth2_data_server/getexamscore?appid=%s&secret=%s&user_name=%s&user_number=%s",
                appid, secret, userName, henauNumber);
        try {
            java.net.http.HttpClient client = java.net.http.HttpClient.newHttpClient();
            java.net.http.HttpRequest request = java.net.http.HttpRequest.newBuilder()
                    .uri(java.net.URI.create(url))
                    .POST(java.net.http.HttpRequest.BodyPublishers.noBody())
                    .build();
            java.net.http.HttpResponse<String> response = client.send(request, java.net.http.HttpResponse.BodyHandlers.ofString());
            return response.body()+"XM\t目标用户的姓名\n" +
                    "YHXH\t目标用户的学/工号\n" +
                    "KCMC\t目标用户当前科目的课程名称\n" +
                    "KCYHDM\t目标用户当前科目的课程代码\n" +
                    "KCLB1\t目标用户当前科目的课程类别（必修/选修）\n" +
                    "XF\t目标用户当前科目的学分\n" +
                    "XDXN\t目标用户当前科目的修读学年\n" +
                    "XDXQ_M\t目标用户当前科目的修读学期\n" +
                    "KSCJ\t目标用户当前科目的考试成绩\n" +
                    "ZYPM\t目标用户当前科目的专业排名\n" +
                    "BJPM\t目标用户当前科目的班级排名\n" +
                    "ZYRS\t目标用户所在专业人数\n" +
                    "BJRS\t目标用户所在班级人数";
        } catch (Exception e) {
            return "查询失败: " + e.getMessage();
        }
    }
}
